(function() {
    angular.module('app').component('darenListComponent', {
        templateUrl: 'app/controllers/redNet.controller/daren.list.component/daren.list.component.html',
        controller: darenListComponent,
        controllerAs: 'vm'
    });

    darenListComponent.$inject = ['darenListComponentService', '$loading', '$rootScope', 'customPaginationService','$controller','$scope','util'];

    function darenListComponent(darenListComponentService, $loading, $rootScope, customPaginationService,$controller,$scope,util) {

        var vm = this;

        vm.$onInit = function(){

            angular.extend(vm, $controller('commonCtrl',{$scope:$scope}));

            //设置分页函数实现
            vm.pager.pagerChange = pagerChange;


            pagerChange();
        };



        function pagerChange(){


            vm.param = util.extend(vm.param, vm.pager);

            $loading.start('darenList');

            darenListComponentService.getDaren(vm.param).then(function(data) {

                vm.pager.setAllPageCount(data.count);

                vm.table.count = data.count;

                vm.table.darens = data.daren;


                $loading.finish('darenList');
            }, function(err) {
                $loading.finish('darenList');
            });
        }


        

        vm.filter = {
            sortColumn: 'popularity',

            darenNick: '',

            sort: 'down'
        };

        // vm.filter = {
        //     sortColumn: 'popularity',

        //     darenNick: '',

        //     sort: 'down',

        //     newPageNumber: 1, //page
        //     count: 10, //条数
        //     begin: 1
        // }

        vm.table = {
            count: 0,
            darens: []
        }

        vm.remove = _remove; //function

        vm.sort = _sort; //function

        vm.pageChanged = _pageChanged; //function

        vm.getDarenShop = _getDarenShop;

        vm.getDarenTie = _getDarenTie;


        vm.pagination = customPaginationService.get();

        vm.darenNickSearch = _darenNickSearch;//function

        vm.remove = _remove;//function

        vm.Enter = _keyEnter;//function

        // console.log(vm.pagination);
        
        //vm.pageChanged(1);//分页第一页


        function _init() {

            var json = angular.extend({},vm.filter,vm.pagination);

            $loading.start('darenList');

            darenListComponentService.getDaren(json).then(function(data) {

                vm.table.count = data.count;

                vm.table.darens = data.daren;


                $loading.finish('darenList');
            }, function(err) {});

        }

        function _darenNickSearch(){

            vm.filter.darenNick = vm.darenNick;
            // console.log(vm.filter);

            vm.pageChanged(1);
        }

        function _remove() {
            vm.darenNick = '';
            vm.filter.darenNick = '';
            vm.pageChanged(1);
        }

        function _keyEnter(e){
            if (e.keyCode == 13) {
                
                vm.darenNickSearch();
            } else {
                return;
            }
        }

        function _pageChanged(newPageNumber) {
            
            customPaginationService.pageChange(vm.pagination,newPageNumber);
            // console.log(vm.pagination);
            _init();
        }

        function _sort(sortColumn) {

            if (vm.filter.sortColumn == sortColumn) {
                return;
            }
            vm.filter.sortColumn = sortColumn;
            _init();
        }

        function _getDarenShop(daren) {

            $rootScope.$emit('redNet_daren_shop_EV', daren);

        }

        function _getDarenTie(daren) {

            $rootScope.$emit('redNet_daren_tie_EV', daren);
        }


    }
})();
